<?php
class DataModel extends Model {
	protected	$tableName	=	'system_data';
	protected	$list_name	=	'global';
	protected	$fields		=	array (0 => 'id',1 => 'uid',2 => 'list',3 => 'key',4 => 'value',5 => 'mtime','_autoinc' => true,'_pk' => 'id');

	/**
	 * 写入参数列表
	 * @param string $listName 参数列表list
	 * @param array $listData array(key=>value)
	 * @return boolean
	 */
	public function lput($listName='',$listData=array()) {
		//初始化list_name
		$listName	=	$this->_strip_key($listName);
		$result = false;
		//格式化数据
		if(is_array($listData)){
			$insert_sql	.=	"REPLACE INTO __TABLE__ (`list`,`key`,`value`,`mtime`) VALUES ";
			foreach($listData as $key=>$data){
				$insert_sql	.=	" ('$listName','$key','".serialize($data)."','".date('Y-m-d H:i:s')."') ,";
			}
			$insert_sql	=	rtrim($insert_sql,',');
			//插入数据列表
			$result	=	$this->execute($insert_sql);
		}
		$cache_id = '_xdata_lget_' . $listName;
		F($cache_id, null);
		return $result;
	}

	/**
	 * 读取参数列表
	 * @param string $listName 参数列表list
	 * @return array
	 */
	public function lget($list_name='') {
		$list_name = $this->_strip_key($list_name);
		static $_res = array();
		if (isset($_res[$list_name]))
			return $_res[$list_name];
		$cache_id = '_xdata_lget_' . $list_name;
		if (($data = F($cache_id)) === false) {
			$data = array();
			$map['list'] = $list_name;
			$result	= $this->order('id ASC')->where($map)->select();
			if ($result) { 
				foreach($result as $v) {
					$data[$v['key']] = unserialize($v['value']);
				}
			}
			F($cache_id, $data);
		}
		$_res[$list_name] = $data;
		return $_res[$list_name];
	}

	/**
	 * 写入单个数据
	 * @param string $key     要存储的参数list:key
	 * @param string $value   要存储的参数的值
	 * @param boolean $replace false:插入新参数，ture:更新已有参数
	 * @return boolean
	 */
	public function put($key,$value='',$replace=false) {
		$key	=	$this->_strip_key($key);
		$keys	=	explode(':',$key);
		$data	=	serialize($value);
		if($replace){
			$insert_sql	=	"REPLACE INTO __TABLE__ ";
		}else{
			$insert_sql	=	"INSERT INTO __TABLE__ ";
		}
		$insert_sql	.=	"(`list`,`key`,`value`) VALUES ('$keys[0]','$keys[1]','$data') ";
		$result		=	$this->execute($insert_sql);
		$cache_id = '_xdata_lget_' . $keys[0];
		F($cache_id, null);
		return $result;
	}

	/**
	 * 读取数据list:key
	 * @param string $key 要获取的某个参数list:key
	 * @return string
	 */
	public function get($key) {
		$key	=	$this->_strip_key($key);
		$keys	=	explode(':',$key);
		static $_res = array();
		if (isset($_res[$key]))
			return $_res[$key];
		$list = $this->lget($keys[0]);
		return $list[$keys[1]];
	}

	/**
	 * 解析过滤输入
	 * @param array|Object|string $input
	 * @return array
	 */
	protected function _parse_keys($input=''){
		$output	=	'';
		if(is_array($input) || is_object($input)){
			foreach($input as $v){
				$output[]	=	$this->_strip_key($v);
			}
		}elseif(is_string($input)){
			$output[]	=	$this->_strip_key($input);
		}else{
		}
		return $output;
	}

	/**
	 * 过滤key
	 * @param string  $key 只允许格式 数字字母下划线，list:key 不允许出现html代码 和这些符号 ' " & * % ^ $ ? ->
	 * @return string
	 */
	protected function _strip_key($key=''){
		if($key==''){
			return $this->list_name;
		}else{
			$key	=	strip_tags($key);
			$key	=	str_replace(array('\'','"','&','*','%','^','$','?','->'),'',$key);
			return $key;
		}
	}
	
   // 包涵特殊字符的情况下  使用64位字符处理方法
	/**
	 * 写入参数列表
	 * @param string $listName 参数列表list
	 * @param array $listData array(key=>value)
	 * @return boolean
	 */
	public function lput64($listName='',$listData=array()) {
		//初始化list_name
		$listName	=	$this->_strip_key($listName);
		$result = false;
		//格式化数据
		if(is_array($listData)){
			$insert_sql	.=	"REPLACE INTO __TABLE__ (`list`,`key`,`value`,`mtime`) VALUES ";
			foreach($listData as $key=>$data){
				$insert_sql	.=	" ('$listName','$key','".base64_encode(serialize($data))."','".date('Y-m-d H:i:s')."') ,";
			}
			$insert_sql	=	rtrim($insert_sql,',');
			//插入数据列表
			$result	=	$this->execute($insert_sql);
		}
		$cache_id = '_xdata_lget_' . $listName;
		F($cache_id, null);
		return $result;
	}
	

	/**
	 * 读取参数列表
	 * @param string $listName 参数列表list
	 * @return array
	 */
	public function lget64($list_name='') {
		$list_name = $this->_strip_key($list_name);
		static $_res = array();
		if (isset($_res[$list_name]))
			return $_res[$list_name];
		$cache_id = '_xdata_lget_' . $list_name;
		if (($data = F($cache_id)) === false) {
			$data = array();
			$map['list'] = $list_name;
			$result	= $this->order('id ASC')->where($map)->select();
			if ($result) {
				foreach($result as $v) {
					$data[$v['key']] = unserialize(base64_decode($v['value']));
				}
			}
			F($cache_id, $data);
		}
		$_res[$list_name] = $data;
		return $_res[$list_name];
	}
	/**
	 * 写入单个数据
	 * @param string $key     要存储的参数list:key
	 * @param string $value   要存储的参数的值
	 * @param boolean $replace false:插入新参数，ture:更新已有参数
	 * @return boolean
	 */
	public function put64($key,$value='',$replace=false) {
		$key	=	$this->_strip_key($key);
		$keys	=	explode(':',$key);
		$data	=	base64_encode(serialize($value));
		if($replace){
			$insert_sql	=	"REPLACE INTO __TABLE__ ";
		}else{
			$insert_sql	=	"INSERT INTO __TABLE__ ";
		}
		$insert_sql	.=	"(`list`,`key`,`value`) VALUES ('$keys[0]','$keys[1]','$data') ";
		$result		=	$this->execute($insert_sql);
		$cache_id = '_xdata_lget_' . $keys[0];
		F($cache_id, null);
		return $result;
	}
	
	
}
?>